package org.example.message.生产;

import lombok.SneakyThrows;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * @Auther:Liu
 * @Testname:danXiang
 * @Date:2025/8/22 15:03
 */
public class danXiang {
    @SneakyThrows
    public static void main(String[] args) {
        // 1.创建消息生产者 producer，并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("producer_group_a");
        // 2.指定 NameSrv 地址
        producer.setNamesrvAddr("192.168.10.88:9876");
        // 3.启动 producer
        producer.start();
        // 4.创建消息对象，指定 Topic、Tag 和消息体
        for (int i = 0; i < 10; i++) {
            String topic = "topic_a";
            String tag = "tag_a"+i;
            String body = "body_a"+i;
            Message message = new Message(topic, tag, body.getBytes(StandardCharsets.UTF_8));
            // 5.发送消息
           producer.sendOneway(message);
        }

        // 6.关闭生产者 producer

        producer.shutdown();
    }
}
